package com.bupt.controller.common;


import com.bupt.annotation.AccessLimit;
import com.bupt.constants.ResponseResult;
import com.bupt.dto.sys.UserDto;
import com.bupt.service.common.DemandService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Slf4j
@Api(tags = {"项目的业务"})
@RestController
@RequestMapping("/api")
public class DemandController {

    @Resource
    private DemandService demandService;


    @AccessLimit(interfaceName = "创建新的业务")
    @ApiOperation("创建新的业务")
    @PostMapping("/projects/{projectId}/demand")
    public Object addDemand(@ApiIgnore UserDto userDto,
                            @RequestParam("name")String name,
                            @RequestParam("sourceId")String sourceId,
                            @RequestParam("targetId")String targetId,
                            @RequestParam("bandwidth")Double rate,
                            @RequestParam("layer")String layer,
                            @RequestParam("year")Integer year,
                            @RequestParam("type")String type,
                            //@RequestParam(value="nodeMust", required = false )String nodeMust,
                            @RequestParam("protect")String protect,
                            @RequestParam("protectLimit")Integer protectLimit,
                            @PathVariable("projectId")String projectId) {
        try {

            Object o = demandService.addDemand(userDto, name, sourceId, targetId, rate, layer, year, type, protect, protectLimit, projectId);
            return ResponseResult.createOK(o);
        }catch (Exception e){
            System.out.println(e);
            return ResponseResult.createFail(e.getMessage());
        }
    }

    @AccessLimit(interfaceName = "删除业务")
    @ApiOperation("删除项目中的业务")
    @DeleteMapping("/projects/{projectId}/demands/{demandId}")
    public Object deleteDemand(@ApiIgnore UserDto userDto,
                                @PathVariable("projectId")String projectId,
                                @PathVariable("demandId")String demandId){
        try {
            Object o = demandService.deleteDemand(userDto, projectId, demandId);
            return ResponseResult.createOK(o);
        }catch (Exception e){
            return ResponseResult.createFail(e.getMessage());
        }
    }

    @AccessLimit(interfaceName = "更新项目中的业务")
    @ApiOperation("更新项目中的业务")
    @PutMapping("/projects/{projectId}/demands/{demandId}")
    public Object updateDemand(@ApiIgnore UserDto userDto,
                               @PathVariable("projectId")String projectId,
                               @PathVariable("demandId")String demandId,
                               @RequestParam("name")String name,
                               @RequestParam("bandwidth")Double rate,
                               @RequestParam("layer")String layer,
                               @RequestParam("year")Integer year,
                               @RequestParam("type")String type,
                               @RequestParam("protect")String protect,
                               @RequestParam("protectLimit")Integer protectLimit){
        try {
            Object o = demandService.updateDemand(userDto, projectId, demandId, name, rate, layer, year, type, protect, protectLimit);
            return ResponseResult.createOK(o);
        }catch (Exception e){
            return ResponseResult.createFail(e.getMessage());
        }

    }

    @AccessLimit(interfaceName = "获取项目中的业务")
    @ApiOperation("获取项目中的业务")
    @GetMapping("/projects/{projectId}/demands")
    public Object getDemandsList(@ApiIgnore UserDto userDto,
                                 @PathVariable("projectId")String projectId){
        try {
            Object o = demandService.getDemandsList(userDto, projectId);
            return ResponseResult.createOK(o);
        }catch (Exception e){
            return ResponseResult.createFail(e.getMessage());
        }
    }
}
